package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;

import com.bigkoo.alertview.OnItemClickListener;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.UserErweimaActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ErweimaBean;
import com.eqdd.yiqidian.bean.rebuild.UserCardInfoResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.ScanZXingActivity;
import com.eqdd.yiqidian.utils.FileUtil;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.MyCodeUtil;
import com.eqdd.yiqidian.utils.PermissionTipUtil;
import com.eqdd.yiqidian.utils.ShareUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;

import static android.Manifest.permission.CAMERA;

public class UserErweimaActivity extends BaseActivity {
    private UserErweimaActivityCustom dataBinding;
    private Bitmap mBitmap;
    private String name;
    private String guid;
    private ActionSheetDialog bottomChoose;
    private Bitmap printBitmap;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_erweima);
        initTopRightText("更多", v ->
                bottomChoose.show()
        );
        dataBinding.tvTip.setText("在易企点上扫一扫加我");
    }

    @Override
    public void initData() {
        name = getIntent().getStringExtra(Config.NAME);
        guid = getIntent().getStringExtra(Config.GUID);
        if (TextUtils.isEmpty(guid)) {
            guid = user.getGuid();
        }
        if (TextUtils.isEmpty(name)) {
            name = user.getPname();
        }
        OkGo.<HttpResult<UserCardInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.USER_CARD_INFO)
                .params("userGuid", guid)
                .execute(new DialogCallBack<HttpResult<UserCardInfoResultBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<UserCardInfoResultBean>> response) {
                        HttpResult<UserCardInfoResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            initTopTitleBar(View.VISIBLE, httpResult.getItems().getUpname() + "的二维码");
                            ImageUtil.setCircleImage(httpResult.getItems().getPhoto(), dataBinding.ivHead);
                            dataBinding.tvZhiwei.setText(httpResult.getItems().getUname());
                        } else {
                            finish();
                        }
                    }
                });


        initDialog();

    }

    @Override
    public void setView() {
        ErweimaBean erweimaBean = new ErweimaBean();
        erweimaBean.setName(name);
        erweimaBean.setType("2");
        erweimaBean.setUgid(guid);
        MyCodeUtil.createImage(GsonUtils.createGsonString(erweimaBean), 400, new MyCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                mBitmap = bitmap;
                dataBinding.ivErweima.setImageBitmap(bitmap);
                dataBinding.tvName.setText(name);
            }

            @Override
            public void onAnalyzeFailed() {

                System.out.println();
            }
        });
    }

    private void initDialog() {
        bottomChoose = ChooseAlertView.getBottomChoose(UserErweimaActivity.this, null, new String[]{"分享", "保存到手机", "扫描"}, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                switch (position) {
                    case 1:
                        dataBinding.rlPrint.setDrawingCacheEnabled(true);
                        dataBinding.rlPrint.buildDrawingCache();
                        printBitmap = dataBinding.rlPrint.getDrawingCache();
                        File file = FileUtil.saveAsFile(printBitmap);
                        ShareUtil.shareMsgImage(UserErweimaActivity.this, "易企点", "易企点", file.getAbsolutePath());
                        break;
                    case 2:
                        dataBinding.rlPrint.setDrawingCacheEnabled(true);
                        dataBinding.rlPrint.buildDrawingCache();
                        printBitmap = dataBinding.rlPrint.getDrawingCache();
                        saveImageByUniversal(printBitmap);
//                        PictureFileUtils.saveBitmapFile();
//                        File fileErweima = FileUtil.saveImageToGallery(UserErweimaActivity.this, printBitmap);
//                        ToastUtil.showShort("已成功保存到" + fileErweima.getAbsolutePath());
                        break;
                    case 3:
                        RxPermissions.getInstance(UserErweimaActivity.this)
                                .request(CAMERA)
                                .subscribe(granted -> {
                                    if (granted) {
                                        System.out.println("已授权");
                                        Intent intent = new Intent(UserErweimaActivity.this, ScanZXingActivity.class);
                                        startActivityForResult(intent, Config.SCAN);
                                    } else {
                                        PermissionTipUtil.tip(UserErweimaActivity.this, "存储");
                                    }
                                });
                        break;

                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
